package com.prs.crm.dao.oa.impl;

import java.sql.SQLException;
import java.util.Collection;

import org.courser.Util;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.context.Constrains;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.oa.OaTaskSubmitDao;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.OaTask;
import com.prs.crm.domain.oa.OaTaskSubmit;

public class OaTaskSubmitDaoImpl extends BaseDaoImpl<OaTaskSubmit> implements OaTaskSubmitDao{
	
	@SuppressWarnings("unchecked")
	public int countTaskSubmits(final OaTask task)
	{
		return (Integer)this.getHibernateTemplate().execute(
			      new HibernateCallback() {   
						public Object doInHibernate(Session session) throws HibernateException {
				        	Criteria criteria = session.createCriteria(OaTaskSubmit.class);
				        	criteria.setProjection(Projections.count("id"));
				        	criteria.add(Restrictions.eq("task", task));
				        	return criteria.uniqueResult();
				        }   
				 } );
	}

	@Override
	public Collection<OaTaskSubmit> getExecuteTask(Person person) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(OaTaskSubmit.class);
		criteria.add(Restrictions.eq("exePerson", person));
		criteria.add(Restrictions.or(Restrictions.eq("state", Constrains.TASK_EXECUTE), Restrictions.eq("state", Constrains.TASK_NOTPASS)));
		return criteria.list();
	}

	@Override
	public Collection<OaTaskSubmit> getAuditTask(Person person) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(OaTaskSubmit.class);
		criteria.add(Restrictions.eq("state", Constrains.TASK_AUDITING));
		criteria.createAlias("task", "t");
		criteria.add(Restrictions.eq("t.recorder", person));
		return criteria.list();
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<OaTaskSubmit> getOaTaskSubmitByTask(final OaTask oaTask) {
		// TODO Auto-generated method stub
		return (Collection<OaTaskSubmit>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(OaTaskSubmit.class);
				criteria.add(Restrictions.eq("task", oaTask));
				return criteria.list();
			}
			
		});
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<OaTaskSubmit> getOverdueOaTaskSubmit(final Person person) {
		// TODO Auto-generated method stub
		return (Collection<OaTaskSubmit>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(OaTaskSubmit.class);
				criteria.createAlias("task", "t");
				criteria.add(Restrictions.lt("t.endTime", Util.nowFullTime()));
				criteria.add(Restrictions.eq("exePerson", person));
				criteria.add(Restrictions.or(Restrictions.eq("state", Constrains.TASK_EXECUTE), Restrictions.eq("state", Constrains.TASK_NOTPASS)));
				criteria.addOrder(Order.asc("t.endTime"));
				return criteria.list();
			}
			
		});
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<OaTaskSubmit> getExecuteTaskByLevel(final Person person,
			final String level) {
		// TODO Auto-generated method stub
		return (Collection<OaTaskSubmit>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(OaTaskSubmit.class);
				criteria.add(Restrictions.eq("exePerson", person));
				criteria.add(Restrictions.or(Restrictions.eq("state", Constrains.TASK_EXECUTE), Restrictions.eq("state", Constrains.TASK_NOTPASS)));
				criteria.createAlias("task", "t");
				criteria.add(Restrictions.eq("t.level", level));
				return criteria.list();
			}
			
		});
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<OaTaskSubmit> getAuditTaskByLevel(final Person person,
			final String level) {
		// TODO Auto-generated method stub
		return (Collection<OaTaskSubmit>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(OaTaskSubmit.class);
				criteria.add(Restrictions.eq("state", Constrains.TASK_AUDITING));
				criteria.createAlias("task", "t");
				criteria.add(Restrictions.eq("t.recorder", person));
				criteria.add(Restrictions.eq("t.level", level));
				return criteria.list();
			}
			
		});
	}
}
